package realm.inorder.persistence.model;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

@Entity
public class Pedido implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue
	protected Long id;

	@Version
	private Integer version;
	
	@ManyToOne
	@JoinColumn(name = "cliente_fk", nullable = false)
	private Cliente cliente;

	@Temporal(TemporalType.TIMESTAMP)
	private Date dataEfetuado;
	
	@OneToMany(mappedBy = "pedido")
	private Set<Item> items = new HashSet<Item>();
	
	@ManyToOne
	@JoinColumn(name = "usuario_fk", nullable = false)
	private Usuario usuario;

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof Pedido)) {
			return false;
		}
		
		Pedido other = (Pedido)obj;
		
		return new EqualsBuilder()
			.append(this.cliente, other.cliente)
			.append(this.dataEfetuado, other.dataEfetuado)
			.isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder()
			.append(this.cliente)
			.append(this.dataEfetuado)
			.toHashCode();
	}

	@Override
	public String toString() {
		return new StringBuilder()
			.append("[").append(id).append("] Pedido").toString();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public Cliente getCliente() {
		return cliente;
	}

	public void setCliente(Cliente cliente) {
		this.cliente = cliente;
	}

	public Date getDataEfetuado() {
		return dataEfetuado;
	}

	public void setDataEfetuado(Date dataEfetuado) {
		this.dataEfetuado = dataEfetuado;
	}

	public Set<Item> getItems() {
		return items;
	}

	public void setItems(Set<Item> items) {
		this.items = items;
	}

	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}
	

}
